//
//  UIButton2ViewController.m
//  UITest
//
//  Created by wp w on 12-6-11.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIButton2ViewController.h"

@interface UIButton2ViewController ()

@end

@implementation UIButton2ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) toggleButton: (UIButton *) button
{
	if (isOn = !isOn)
	{
		[button setTitle:@"On" forState:UIControlStateNormal];
		[button setTitle:@"On" forState:UIControlStateHighlighted];
		[button setBackgroundImage:baseGreen forState:UIControlStateNormal];
		[button setBackgroundImage:altGreen forState:UIControlStateHighlighted];
	}
	else
	{
		[button setTitle:@"Off" forState:UIControlStateNormal];
		[button setTitle:@"Off" forState:UIControlStateHighlighted];
		[button setBackgroundImage:baseRed forState:UIControlStateNormal];
		[button setBackgroundImage:altRed forState:UIControlStateHighlighted];
	}
}


- (void) viewDidLoad
{
	self.title = @"Toggle Button";
	
	float capWidth = 110.0f;
	baseGreen = [[[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:capWidth topCapHeight:0.0f] retain];
	baseRed = [[[UIImage imageNamed:@"red.png"] stretchableImageWithLeftCapWidth:capWidth topCapHeight:0.0f] retain];
	altGreen = [[[UIImage imageNamed:@"green2.png"] stretchableImageWithLeftCapWidth:capWidth topCapHeight:0.0f] retain];
	altGreen = [[[UIImage imageNamed:@"red2.png"] stretchableImageWithLeftCapWidth:capWidth topCapHeight:0.0f] retain];
	
	// Create a button sized to our art
	UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
	button.frame = CGRectMake(0.0f, 0.0f, 220.0f, 233.0f);
	button.center = CGPointMake(160.0f, 140.0f);
	
	// Set up the button aligment properties
	button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
	button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
	
	// Set the font and color
	[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
	button.titleLabel.font = [UIFont boldSystemFontOfSize:24.0f];
	
	// Add action
	[button addTarget:self action:@selector(toggleButton:) forControlEvents: UIControlEventTouchUpInside];
	
	// Load Butterflies
	NSMutableArray *bflies = [NSMutableArray array];
	UIImage *img;
	for (int i = 1; i <= 17; i++) {
		NSString *bfname = [NSString stringWithFormat:@"bf_%d.png", i];
		if (img = [UIImage imageNamed:bfname]) 
            [bflies addObject:img];
	}
	
	UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];
	[imageView setAnimationImages:bflies];
	[imageView setAnimationDuration:1.2f];
	[imageView startAnimating];
	imageView.center = button.center;
	
	// For tracking the two states
	isOn = NO;
	[self toggleButton:button];
    
	// Place the button into the view
	[self.view addSubview:imageView];
	[self.view addSubview:button];	
}

- (void) dealloc
{
	[baseGreen release];
	[altGreen release];
	[baseRed release];
	[altRed release];
    
	[super dealloc];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
